<template>
  <div class="dashboard">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>问卷总数</span>
            </div>
          </template>
          <div class="card-body">
            <div class="number">{{ stats.questionnaireCount }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>用户总数</span>
            </div>
          </template>
          <div class="card-body">
            <div class="number">{{ stats.userCount }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>今日提交</span>
            </div>
          </template>
          <div class="card-body">
            <div class="number">{{ stats.todaySubmitCount }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>总访问量</span>
            </div>
          </template>
          <div class="card-body">
            <div class="number">{{ stats.totalVisits || 0 }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const stats = ref({
  questionnaireCount: 0,
  userCount: 0,
  todaySubmitCount: 0,
  totalVisits: 0
})

const loadStats = async () => {
  try {
    const res = await request.get('/api/dashboard/stats')
    if (res.code === 200) {
      stats.value = res.data
    } else {
      ElMessage.error(res.msg || '获取统计数据失败')
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    ElMessage.error('获取统计数据失败')
  }
}

onMounted(() => {
  loadStats()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-body {
  text-align: center;
  padding: 20px;
}

.number {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}
</style> 